<template>
  <div>
    <el-dialog
      append-to-body
      title="框架所属亚马逊产品"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      class="mt-dialog"
      width="80%"
      top="10px;"
      @close="close"
    >
      <div class="top">
        <el-input
          v-model="kwInput"
          placeholder="输入 任意字符 进行筛选"
          class="input-with-select"
          clearable
          @keyup.native.enter="handleSearch"
        >
          <el-button slot="append" icon="el-icon-search" @click="handleSearch">搜索</el-button>
        </el-input>
        <!--        <el-button type="primary" class="el-icon-plus add-btn" @click="handleAction('create', '添加站点')"-->
        <!--                   :disabled="listLoading">添加站点-->
        <!--        </el-button>-->
      </div>
      <el-table
        v-loading="listLoading"
        :data="data"
        border
        fit
        highlight-current-row
        style="width: 100%"
        @sort-change="handleSort"
      >
        <el-table-column label="keyword" prop="topic__keyword" sortable="custom" width="120" />
        <el-table-column label="post" prop="post_id" width="100" />
        <el-table-column label="topic title" prop="topic__title" />
        <el-table-column label="爬取状态" prop="is_finished" width="100">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.is_finished"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="handleModifyFinishedStatus(scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column label="已发博客">
          <template slot-scope="scope">
            <ol style="overflow:auto;height: 100px">
              <li
                v-for="item in scope.row.post_urls"
                :key="item.asin"
                class="infinite-list-item"
                :value="item.asin"
              >{{ item.url }}</li>
            </ol>
          </template>
        </el-table-column>
        <el-table-column align="left" label="products" sortable="custom">
          <template slot-scope="scope">
            <ol style="overflow:auto;height: 100px">
              <li
                v-for="item in scope.row.products"
                :key="item.asin"
                :value="item.asin"
              >{{ item.url }}</li>
            </ol>
          </template>
        </el-table-column>
        <el-table-column align="right" label="操作" sortable="custom" width="260">
          <template slot-scope="scope">
            <el-button size="mini" type="danger" icon="el-icon-delete" @click="handleAction('delete', scope.row)">删除</el-button>
            <el-button v-if="scope.row.products.length >0 " size="mini" type="success" icon="el-icon-plus" @click="handleAddReview(scope.row)">加入review</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        v-if="data.length"
        background
        class="mt-pagination"
        :current-page="page"
        :page-sizes="[8,16,32,100]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handlePageChange"
      />
    </el-dialog>
  </div>
</template>

<script>
import { Get, Post, Put, Delete } from '@/api/cms/topicamazonproduct'
import qs from 'qs'

const orderMapper = {
  'ascending': 0,
  'descending': 1,
  'null': ''
}
export default {
  name: 'TopicAmazonProduct',
  props: ['webVisible', 'search_query'],
  data() {
    return {
      viewDetail: false,
      viewTitle: '',
      viewdescription: '',
      activeName: 'first',
      data: [],
      listLoading: false,
      kwInput: '',
      order_by: '',
      order_desc: '',
      // pagination
      pagesize: 8,
      page: 1,
      total: 0,
      topic_data: {
        title: '',
        description: ''
      },
      action: null,
      dialogVisible: false,
      title: '',
      temp: {
        id: ''
      },
      width: '',
      projects: [],
      innerDialogVisible: false
    }
  },
  watch: {
    search_query(v) {
      console.log('query', v)
      if (v) {
        this.kwInput = v
      }
    },
    webVisible(v) {
      this.dialogVisible = v
      if (v) {
        this.kwInput = this.search_query
        this.getList()
      }
    }
  },
  methods: {
    resetTemp() {
      this.temp = {
        description: '',
        title: ''
      }
      // console.log('create')
      // this.$nextTick(() => {
      //   this.$refs.tmmaterial.setContent('')
      // })
    },
    handleViewDetail(row) {
      this.viewTitle = row.title
      this.viewdescription = row.description
      this.viewDetail = true
    },
    getList() {
      this.listLoading = true
      Get(null, qs.stringify({
        kw: this.kwInput,
        pagesize: this.pagesize,
        page: this.page,
        topic_id: this.topic_id,
        order_by: this.order_by,
        order_desc: this.order_desc
      }))
        .then(response => {
          this.data = response.data.data
          this.listLoading = false
          this.total = response.data.total
        }).catch(err => {
          console.log(err)
          this.listLoading = false
        })
    },
    handleAddReview(row) {
      const urls = row.products.map(item => item.url)
      this.$emit('add_products', { urls: urls, post_id: row.post_id, keyword: row.topic__keyword })
      this.dialogVisible = false
    },
    handleModifyFinishedStatus(row) {
      Put(row.id, {
        is_finished: row.is_finished
      }).then((response) => {
        this.getList()
        this.$message.success('更新成功')
      })
    },
    handleAction(action, row) {
      this.action = action
      switch (action) {
        case 'create':
          this.width = '50%'
          this.title = row
          this.resetTemp()
          this.activeName = 'second'
          break
        case 'delete':
          this.width = '30%'
          Delete(row.id).then((response) => {
            this.getList()
            this.notify('删除成功')
          })
          break
        case 'edit':
          this.title = `修改Post: ${row.title}`

          this.temp = Object.assign({}, row)
          this.activeName = 'second'
          break
      }
    },
    confirm() {
      this.temp.topic_id = this.topic_id
      const _data = this.temp
      switch (this.action) {
        case 'create':
          if (_data.topic_id === '') {
            alert('必须选择话题')
            return
          }
          Post({
            post_id: _data.post_id,
            topic_id: _data.topic_id
          }
          ).then(response => {
            this.innerDialogVisible = false
            this.activeName = 'first'
            this.getList()
            // this.notify('添加成功')
          })
          break
        case
          'delete'
          :
          Delete(_data.id).then((response) => {
            this.innerDialogVisible = false
            this.activeName = 'first'
            this.getList()
            this.notify('删除成功')
          })
          break
        case
          'edit'
          :
          Put(_data.id, {
            // title: _data.title,
            // description: _data.description,
            post_id: _data.post_id,
            topic_id: _data.topic_id
          }).then((response) => {
            this.innerDialogVisible = false
            this.activeName = 'first'
            this.getList()
            this.notify('更新成功')
          })
          break
      }
    },
    notify(message, duration) {
      this.$notify({
        title: '成功',
        message: message,
        type: 'success',
        duration: duration || 2000
      })
    },
    // pageSize 改变后触发 table 更新
    handleSizeChange(val) {
      this.pagesize = val
      this.getList()
    },
    // page 改变后触发 table 更新
    handlePageChange(val) {
      this.page = val
      this.getList()
    },
    // 点击搜索
    handleSearch() {
      this.page = 1
      this.getList()
    },
    handleSort(v) {
      this.order_by = v.prop
      this.order_desc = orderMapper[v.order]
      this.getList()
    },
    scrollTop() {
      window.scrollTo(0, 0)
    },
    close() {
      this.$emit('close', false)
    }
  }
  // created() {
  //   this.handleFetchTopic()
  // }
}
</script>

<style scoped>
  .el-tag + .el-tag {
    margin-left: 10px;
  }

  .top >>> .el-select .el-input {
    width: 350px;
  }

  .top >>> .input-with-select .el-input-group__prepend {
    background-color: #fff;
  }

  .top {
    padding-left: 0px;
    margin-bottom: 5px;
    margin-top: 2px;
  }

  .input-with-select {
    width: calc(100% - 200px);
  }

  .add-btn {
    float: right;
    margin-right: 20px;
    width: 150px
  }

  .mt-pagination {
    float: right;
    margin-top: 5px;
  }

  .app-container >>> .el-dialog {
    margin-top: 48px !important;
    margin-bottom: 68px;
  }

  .mt-dialog >>> .el-transfer-panel {
    width: calc(50% - 60px);
  }

  .mt-form >>> .el-select {
    width: 100%;
  }

  .mt-form >>> .el-input__inner {
    width: 100%;
  }

  .mt-dialog >>> .el-dialog {
    margin: 10px auto !important;
  }

  .mt-dialog >>> .el-dialog__body {
    padding-bottom: 100px;
    padding-top: 5px;
  }

  .mt-dialog >>> .el-dialog__header {
    padding-bottom: 5px;
    padding-top: 10px;
  }
</style>
